//
//  HomeViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/7/17.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeViewModel.h"
#import "chooseNumTitleView.h"
#import "GreatGodView.h"
#import "DoubleBallViewController.h"
#import "PickBallNewController.h"
#import "ChooseNumCollectionCell.h"
#import "NewsTableViewCell.h"
#import "BannerViewController.h"
#import "NewsDetailsViewController.h"

#import "PowerViewController.h"
#import "EvenRedViewController.h"
#import "RewardViewController.h"

@interface HomeViewController ()<UIScrollViewDelegate,SDCycleScrollViewDelegate,GreatGodViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray* bannerArray;
    NSArray* titleArr;
    NSArray* imageArr;
}

@property(nonatomic,strong)UIScrollView* mainScroll;
@property(nonatomic,strong)SDCycleScrollView *topScroll;
@property(nonatomic,strong)HomeViewModel * homeViewModel;
@property(nonatomic,strong)GreatGodView* greatGodView;
@property(nonatomic,strong)chooseNumTitleView* chooseNumTitleView;
@property(nonatomic,strong)chooseNumTitleView* newsTitleView;
@property(nonatomic,strong)UICollectionView* chooseNumCollect;
@property(nonatomic,strong)UITableView* newsTable;
@property(nonatomic,strong)UIView * tempView;
@end

@implementation HomeViewController

- (void)viewWillDisappear:(BOOL)animated{
    self.topScroll = nil;
}

-(HomeViewModel *) homeViewModel{
    if (!_homeViewModel) {
        _homeViewModel = [[HomeViewModel alloc] init];
    }
    return _homeViewModel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initScroll];
    [self setupRefresh];
    [self createTempView];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
}

-(void)createTempView{
    
    self.tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.newsTable.width, self.newsTable.height)];
    
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake((CGRectGetWidth(self.tempView.frame) - 50)/2, (CGRectGetHeight(self.tempView.frame) - 154)/2, 50, 50)];
    imageview.image = [UIImage imageNamed:@"wushuju"];
    imageview.contentMode = UIViewContentModeScaleAspectFit;
    [self.tempView addSubview:imageview];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,imageview.height+10 + imageview.y, CGRectGetWidth(self.tempView.frame), 30)];
    label.textColor = DEF_COLOR_RGB(215, 215, 215);
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:15.f];
    label.text = @"暂无数据，我们会尽快添加";
    [self.tempView addSubview:label];
    
    [self.newsTable addSubview:self.tempView];
    self.tempView.hidden = YES;
}

-(void)setupRefresh{
    
    self.mainScroll.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    [self.mainScroll.mj_header beginRefreshing];
    
}

-(void)refresh{
    
    [self getBanner];
}

-(void)getBanner{
    
    [MXUtility showMBProgress:self.view message:LoadingText];
    
    [[self.homeViewModel mainBannerRequestpParameter:@"0" pageSize:@"5"] subscribeNext:^(id result) {
        
        if([result[@"errorcode"] integerValue] == 0){
            
            [self getMainNewsData];
            
        }else{
            
            [MXUtility showTips:@"获取bannner失败"];
            
        }
        
        [MXUtility hideMBProgress:self.view];
        
    }error:^(NSError *error) {
        
        [MXUtility hideMBProgress:self.view];
        [self.mainScroll.mj_header endRefreshing];
    }];
    
}

-(void)getMainNewsData{
    
    [MXUtility showMBProgress:self.view message:LoadingText];
    
    [[self.homeViewModel mainNewsRequestpParameter:@"0" pageSize:@"5" typeId:@"1"] subscribeNext:^(id result) {
        
        if([result[@"errorcode"] integerValue] == 0){
            
            if (self.homeViewModel.homeArrayList.count==0) {
                
                self.tempView.hidden = NO;
                
            }else{
                
                self.tempView.hidden = YES;
            }
            [self initUI];
            [self.newsTable reloadData];
            
        }else{
            
            [MXUtility showTips:@"获取新闻列表失败"];
            
        }
        
        [MXUtility hideMBProgress:self.view];
        [self.mainScroll.mj_header endRefreshing];
    }error:^(NSError *error) {
        
        [MXUtility hideMBProgress:self.view];
        [self.mainScroll.mj_header endRefreshing];
        
    }];
    
    
    
    
}
-(void)initScroll{
    
    self.mainScroll=[UIScrollView new];
    self.mainScroll.delegate=self;
    self.mainScroll.showsVerticalScrollIndicator=NO;
    self.mainScroll.backgroundColor=DEF_COLOR_F0F0F0;
    [self.view addSubview:self.mainScroll];
    [self.mainScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH);
        make.height.mas_equalTo(DEF_DEVICE_HEIGHT-49-64);
        make.top.equalTo(self.view).with.offset(0);
        make.leading.equalTo(self.view).with.offset(0);
    }];
    
}

-(void)initUI{
    
    titleArr=@[@"六合彩",@"快乐十分",@"幸运飞艇",@"PK10",@"快3",@"北京28",@"PC蛋蛋",@"北京11选5",@"3D试验机"];
    imageArr=@[@"tupian3.png",@"tupian4.png",@"tupian5.jpg",@"tupian7.png",@"tupian2.png",@"tupian1.png",@"tupian6.png",@"tupian8.png",@"tupian9.png"];
    
    NSMutableArray* imageArray=[NSMutableArray array];
    for (MainBannerModel* model in self.homeViewModel.bannerList) {
        
        [imageArray addObject:model.imgUrl];
        
    }
    bannerArray=[imageArray copy];
    
    self.topScroll = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, DEF_DEVICE_WIDTH, DEF_DEVICE_WIDTH*230/414) shouldInfiniteLoop:YES imageNamesGroup:bannerArray];
    self.topScroll.delegate = self;
    self.topScroll.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    [self.mainScroll addSubview:self.topScroll];
    self.topScroll.autoScrollTimeInterval = 2.5;
    self.topScroll.currentPageDotColor=base_COLOR_FFA54F;
    [self.mainScroll addSubview:self.topScroll];
    
    
    //    大神
    self.greatGodView=[GreatGodView new];
    self.greatGodView.delegate=self;
    [self.mainScroll addSubview:self.greatGodView];
    [self.greatGodView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH);
        make.height.mas_equalTo(119+10);
        make.top.equalTo(self.topScroll.mas_bottom).with.offset(10);
        make.leading.equalTo(self.mainScroll).with.offset(0);
    }];
    
    self.chooseNumTitleView=[chooseNumTitleView new];
    self.chooseNumTitleView.faxed.text=@"幸运摇号";
    [self.mainScroll addSubview:self.chooseNumTitleView];
    [self.chooseNumTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH);
        make.height.mas_equalTo(29);
        make.top.equalTo(self.greatGodView.mas_bottom).with.offset(10);
        make.leading.equalTo(self.mainScroll).with.offset(0);
    }];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    
    layout.itemSize = CGSizeMake(iconWeight+20, iconWeight+20);
    
#define chooseNumCollectHeight layout.itemSize.height*3+60
    
    self.chooseNumCollect=[[UICollectionView alloc]initWithFrame:CGRectMake(0, DEF_DEVICE_WIDTH*230/414+10+119+10+29+10, DEF_DEVICE_WIDTH, chooseNumCollectHeight) collectionViewLayout:layout];
    [self.mainScroll addSubview:self.chooseNumCollect];
    [self.chooseNumCollect registerClass:[ChooseNumCollectionCell class] forCellWithReuseIdentifier:@"cellid"];
    self.chooseNumCollect.delegate=self;
    self.chooseNumCollect.dataSource=self;
    self.chooseNumCollect.allowsMultipleSelection = NO;
    self.chooseNumCollect.scrollEnabled=NO;
    self.chooseNumCollect.backgroundColor=[UIColor whiteColor];
    
    self.newsTitleView=[[chooseNumTitleView alloc]initWithFrame:CGRectMake(0, DEF_DEVICE_WIDTH*230/414+10+119+10+29+10+chooseNumCollectHeight+10, DEF_DEVICE_WIDTH, 29)];
    self.newsTitleView.faxed.text=@"彩市花边";
    [self.mainScroll addSubview:self.newsTitleView];
    
    //    5条新闻
    self.newsTable=[UITableView new];
    self.newsTable.delegate=self;
    self.newsTable.dataSource=self;
    self.newsTable.scrollEnabled=NO;
    self.newsTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.mainScroll addSubview:self.newsTable];
    [self.newsTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH);
        make.height.mas_equalTo(425);
        make.top.equalTo(self.newsTitleView.mas_bottom).with.offset(0);
        make.leading.equalTo(self.mainScroll).with.offset(0);
    }];
    
    
    
    if(iPhoneX){
        
        float height=DEF_DEVICE_WIDTH*230/414+10+119+10+29+10+chooseNumCollectHeight+10+29+425+10+50+10;
        [self.mainScroll setContentSize:CGSizeMake(DEF_DEVICE_WIDTH, height)];
        
    }else{
        
        float height=DEF_DEVICE_WIDTH*230/414+10+119+10+29+10+chooseNumCollectHeight+10+29+425+10;
        [self.mainScroll setContentSize:CGSizeMake(DEF_DEVICE_WIDTH, height)];
        
    }
    
    
}

#pragma mark - 导航图片的点击
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    MainBannerModel* model=self.homeViewModel.bannerList[index];
    
    BannerViewController* vc=[[BannerViewController alloc]init];
    vc.createTime=model.createTime;
    vc.Description=model.Description;
    vc.imgId=model.contentId;
    vc.imgTitle=model.title;
    vc.imgUrl=model.imgUrl;
    vc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    NSLog(@"---点击了第%ld张图片", (long)index);
    
}

- (void)shiLiButClick{
    
    PowerViewController * power = [[PowerViewController alloc] init];
    power.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:power animated:YES];
}

- (void)jiangBeiButClick{
    
    RewardViewController * rewar = [[RewardViewController alloc] init];
    rewar.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:rewar animated:YES];
    
}

- (void)lianHongButtClick{
    
    EvenRedViewController * even = [[EvenRedViewController alloc] init];
    even.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:even animated:YES];
    
}

// 返回的区数
-(NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 9;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ChooseNumCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellid" forIndexPath:indexPath];
    
    cell.name.text=titleArr[indexPath.row];
    cell.iconImage.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",imageArr[indexPath.row]]];
    return cell;
}

#define spacing (DEF_DEVICE_WIDTH-(iconWeight+20)*3)/4
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    
    return UIEdgeInsetsMake(0, spacing, 10, spacing);//分别为上、左、下、右
}
//header的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(10, 10);
}

#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DoubleBallViewController *VC = [[DoubleBallViewController alloc]init];
    PickBallNewController * Vc2 = [[PickBallNewController alloc]init];
    if (indexPath.row == 0) {
        
        VC.Navtitle = @"双 色 球";
        
        VC.textArray = @[@"请选择 6 个红球",@"请选择 1 个蓝球"];
        VC.R_count = 33;
        VC.B_count = 16;
        VC.R_count_min = 6;
        VC.B_count_min = 0;
        
        //        49 160 228
      [self.navigationController pushViewController:VC animated:YES];
        
        NSLog(@"双色球");
    }else if (indexPath.row == 1){
        
        VC.Navtitle = @"超级大乐透";
        VC.textArray = @[@"请选择 5 个红球",@"请选择 2 个蓝球"];
        VC.R_count = 35;
        VC.B_count = 12;
        VC.R_count_min = 5;
        VC.B_count_min = 1;
        
        NSLog(@"大乐透");
        [self.navigationController pushViewController:VC animated:YES];
    }else if (indexPath.row == 2){
        
        [Vc2 ContrillerName:@"七 星 彩" MethodsName:@"任选7个号" SectionCount:1 RowCount:30 RandomCount: 7 OtheParameter:@"qxc" SectionNameArray: @[@"选  号"]];
        
        [self.navigationController pushViewController:Vc2 animated:YES];
        
        NSLog(@"七乐彩");
    }else if (indexPath.row == 3){
        
        [Vc2 ContrillerName:@"3D 福 彩" MethodsName:@"直  选" SectionCount:3 RowCount:10 RandomCount:1 OtheParameter:@"sd" SectionNameArray: @[@"百  位",@"十  位",@"个  位"]];
        
        [self.navigationController pushViewController:Vc2 animated:YES];
        NSLog(@"3D福彩");
        
    }else if (indexPath.row == 4){
        
        [Vc2 ContrillerName:@"排 列 三" MethodsName:@"三星 - 直选" SectionCount:3 RowCount:10 RandomCount:1 OtheParameter:@"pls" SectionNameArray:@[@"百  位",@"十  位",@"个  位"]];
        
        [self.navigationController pushViewController:Vc2 animated:YES];
        NSLog(@"排列三");
        
    }else if (indexPath.row == 5){
        
        [Vc2 ContrillerName:@"排 列 五" MethodsName:@"五星 - 直选" SectionCount:5 RowCount:10 RandomCount:1 OtheParameter:@"plw"  SectionNameArray: @[@"万  位",@"千  位",@"百  位",@"十  位",@"个  位"]];
        
        [self.navigationController pushViewController:Vc2 animated:YES];
        
        NSLog(@"排列五");
        
    }else if (indexPath.row == 6){
        
        [Vc2 ContrillerName:@"重庆时时彩" MethodsName:@"0-9任选5位" SectionCount:1 RowCount:10 RandomCount:5 OtheParameter:@"cqssc" SectionNameArray:@[@"选号"]];
        
        [self.navigationController pushViewController:Vc2 animated:YES];
        NSLog(@"时时彩");
        
    }else if (indexPath.row == 7){
        
        [Vc2 ContrillerName:@"安徽 11选5" MethodsName:@"01～11任选5位" SectionCount:1 RowCount:11 RandomCount:5 OtheParameter:@"syxw" SectionNameArray: @[@"选号"]];
        
        [self.navigationController pushViewController:Vc2 animated:YES];
        NSLog(@"大11选5");
        
    }else{
        
        [Vc2 ContrillerName:@"3D 试验机" MethodsName:@"直  选" SectionCount:3 RowCount:10 RandomCount:1 OtheParameter:@"sdsjh" SectionNameArray: @[@"百  位",@"十  位",@"个  位"]];
        
        [self.navigationController pushViewController:Vc2 animated:YES];
        NSLog(@"3D 试验机");
        
    }
    
    //    NSLog(@"点击了第 %zd组 第%zd个",indexPath.section, indexPath.row);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.homeViewModel.homeArrayList.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"mainNewsCell";
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        cell = [[NewsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.model=self.homeViewModel.homeArrayList[indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MainNewsModel* model=self.homeViewModel.homeArrayList[indexPath.row];
    //    新闻详情页
    NewsDetailsViewController* vc=[[NewsDetailsViewController alloc]init];
    vc.hidesBottomBarWhenPushed=YES;
    vc.controllerFromWhere=UIViewControllerFromOther;
    vc.newsId=model.newsId;
    [self.navigationController pushViewController:vc animated:YES];
    
}

@end

